<?php
/**
 * @package App
 * @subpackage resource
 */
 
/**
 *
 * rakuten API リソース
 * 
 * http://webservice.rakuten.co.jp/
 *
 * @package     App
 * @subpackage  resource
 * @author      $Author: moriwaki $
 * @version     $Id: rakuten.php 360 2008-10-1 01:58:40Z moriwaki $
 * 
 */
class Rakuten extends BEAR_Vo
{
 
    /**
     * API uri
     */
    private static $uri = "http://api.rakuten.co.jp/rws/1.7/rest";

	/**
	 * rakuten api key
	 */
	private static $apiKey = '';
	
	/**
	 * rakuten アフィリエイトID
	 */
	private static $affiliateId = '';
	
    /**
     * 作成
     * 
     * @param array $values
     * + name: string 名前
     * @return integer id or error
     */
    public function onCreate($values)
    {
    }
 
    /**
     * 変更
     * 
     * @param array $values
     * + id: integer $id
     * + name: string 名前
     * @return integer id or error
     */
    public function onUpdate($values)
    {
    }
 
    /**
     * 読み込み
     * 
     * @param array $values
     * + mode: strign $mode
     * @return array
     */
    public function onRead($values)
    {
        //assert & mapping
        self::assert(isset($values['mode']));
        $mode = $values['mode'];
	    $option = array(
	    "timeout" => "3", // タイムアウトの秒数指定
	    );
	    $params = array();
	    $params['developerId'] = self::apiKey;
	    $params['affiliateId'] = self::affiliateId;
	    if (App::$ua == BEAR_Agent::UA_PC){
	        $params['carrier'] = 0;
	    }else{
	        $params['carrier'] = 1;
	    }
	
        switch($mode){
	    case 'genresearch' :
	        $params['operation'] = 'GenreSearch';
	        $params['version'] = '2007-04-11';
	        $params['genreId'] = App_Values::getValue('genreId', $values, 0);

	        $p = new App_Url(self::$uri, $params);
	        $url = $p->getUrl();
	        $req=& new HTTP_Request($url, $option);
	        $req->sendRequest();
	        $result = $req->getResponseBody();

	        $parser = new XML_Unserializer(array('parseAttributes' => true));
	        $parser->unserialize($result);
	        $result = $parser->getUnserializedData();

	        if (is_array($result) && $result['header:Header']['Status'] == 'Success'){
	            $result = $result['Body']['genreSearch:GenreSearch'];
	        }else{
	            $result = array();
	        }

	        break;

	    case 'itemsearch' :
	        $params['availability'] = 1;
	        $params['operation'] = 'ItemSearch';
	        $params['version'] = '2007-04-11';
	        $params['genreId'] = App_Values::getValue('genreId', $values, 0);
	        $shopCode = App_Values::getValue('shopCode', $values);
	        if ($shopCode){
	            $params['shopCode'] = $shopCode;
	        }
	        $keyword = App_Values::getValue('keyword', $values);
	        if ($keyword){
	            $params['keyword'] = $keyword;
	        }

	        $params['sort'] = '+updateTimestamp';
	        $params['hits'] = '10';
	        $params['page'] = App_Values::getValue('page', $values, 1);

	        $p = new App_Url(self::$uri, $params);
	        $url = $p->getUrl();
	        $req=& new HTTP_Request($url, $option);
	        $req->sendRequest();
	        $result = $req->getResponseBody();

	        $parser = new XML_Unserializer(array('parseAttributes' => true));
	        $parser->unserialize($result);
	        $result = $parser->getUnserializedData();

	        if (isset($result['Body']['itemSearch:ItemSearch']['Items']['Item']['itemCode'])){
	            $tmp = $result['Body']['itemSearch:ItemSearch']['Items']['Item'];
	            $result['Body']['itemSearch:ItemSearch']['Items']['Item'] = array();
	            $result['Body']['itemSearch:ItemSearch']['Items']['Item'][] = $tmp;
	        }
	        if (is_array($result) && $result['header:Header']['Status'] == 'Success'){
	            $result = $result['Body']['itemSearch:ItemSearch'];
	        }else{
	            $result = array();
	        }

	        break;

	    case 'itemcodesearch' :
	        $params['availability'] = 1;
	        $params['operation'] = 'ItemCodeSearch';
	        $params['version'] = '2007-04-11';
	        $params['itemCode'] = App_Values::getValue('itemCode', $values);

	        $p = new App_Url(self::$uri, $params);
	        $url = $p->getUrl();
	        $req=& new HTTP_Request($url, $option);
	        $req->sendRequest();
	        $result = $req->getResponseBody();

	        $parser = new XML_Unserializer(array('parseAttributes' => true));
	        $parser->unserialize($result);
	        $result = $parser->getUnserializedData();

	        if (is_array($result) && $result['header:Header']['Status'] == 'Success'){
	            $result = $result['Body']['itemCodeSearch:ItemCodeSearch']['Item'];
	        }else{
	            $result = array();
	        }
	        break;
        }

        return $result;
    }
 
    /**
     * 削除
     * 
     * @param array $values
     * + id: integer id
     * @return MDB2_Result
     */
    public function onDelete($values)
    {
    }
}
